<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('水果列表')" />
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>水果种类编号：</label>
                                <input type="text" id="fruittypesId" name="fruittypesId"/>
                            </li>
                            <li>
                                <label>水果种类名称：</label>
                                <input type="text" id="fruittypesName" name="fruittypesName"/>
                            </li>
                            <li>
                                <label>水果编号：</label>
                                <input type="text" name="code"/>
                            </li>
                            <li>
                                <label>水果名称：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <label>水果状态：</label>
                                <select name="isStatus" th:with="type=${@dict.getType('fruits')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.addTab()" shiro:hasPermission="inventory:fruits:add">
                    <i class="fa fa-plus"></i> 添加水果
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="inventory:fruits:remove">
                    <i class="fa fa-remove"></i> 批量下架
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('inventory:fruits:edit')}]];
        var removeFlag = [[${@permission.hasPermi('inventory:fruits:remove')}]];
        var isStatusDatas = [[${@dict.getType('fruits')}]];
        var prefix = ctx + "inventory/fruits";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "水果",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'fruitId',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1
                    }
                },
                {
                    field: 'fruittypesId',
                    title: '水果种类编号',
                    visible: false
                },
                {
                    title: '水果种类名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectFruitTypeNameByFruitTypeId(row.fruittypesId, index)
                        })();
                        return ('<span id=' + "fruitTypeNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'code',
                    title: '水果编号'
                },
                {
                    field: 'name',
                    title: '水果名称'
                },
                {
                    field: 'img',
                    title: '水果图片',
                    formatter: function(value, row, index) {
                        // 图片预览（注意：如存储在本地直接获取数据库路径，如有配置context-path需要使用ctx+路径）
                        // 如：/profile/upload/2019/08/08/3b7a839aced67397bac694d77611ce72.png
                        if(index % 2 === 0){
                            return $.table.imageView(row.img);
                        }else {
                            return $.table.imageView(row.img);
                        }
                    }
                },
                {
                    field: 'createdBy',
                    title: '创建人'
                },
                {
                    field: 'createdTime',
                    title: '创建时间'
                },
                {
                    field: 'updatedBy',
                    title: '更新人'
                },
                {
                    field: 'updatedTime',
                    title: '更新时间'
                },
                {
                    field: 'isStatus',
                    title: '水果状态',
                    formatter: function(value, row, index) {
                        if (value === 1) {
                            return ('<i class="fa fa-toggle-on text-info fa-2x" onclick="fruitBan(' + row.fruitId + ',' + 0 + ')"></i>')
                        } else {
                            return ('<i class="fa fa-toggle-off text-info fa-2x" onclick="fruitBan(' + row.fruitId + ',' + 1 + ')"></i>')
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.fruitId + '\')"><i class="fa fa-edit"></i>修改水果信息</a> ');
                        // actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.fruitId + '\')"><i class="fa fa-remove"></i>下架</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>

<!--  根据水果种类名称查询水果种类编号  -->
    <script>
        $(document).ready(function () {
            $("#fruittypesName").on('input', function () {
                var fruittypesName = $(this).val();
                if (fruittypesName) {
                    selectFruitTypeIdByFruitTypeName(fruittypesName);
                } else {
                    $("#fruittypesId").val(""); // 如果输入框为空就清空水果种类编号输入框
                }
            })
        })
        function selectFruitTypeIdByFruitTypeName(fruittypesName) {
            $.ajax({
                url: "/inventory/fruits/selectFruitTypeId",
                dataType: "json",
                data: {
                    "name": fruittypesName
                },
                type: "post",
                success: function (resp) {
                    if (resp.fruitTypeId != null && resp.fruitTypeId !== "") {
                        $("#fruittypesId").val(resp.fruitTypeId);
                    } else {
                        $("#fruittypesId").val(""); // 如果查不到就清空水果种类编号输入框
                    }
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
    </script>

<!--  根据水果种类编号查询水果种类名称  -->
     <script>

         $(document).ready(function () {
             $("#fruittypesId").on('input', function () {
                 var fruittypesId = $(this).val();
                 if (fruittypesId) {
                     selectFruitTypeNameByFruitTypeId(fruittypesId);
                 } else {
                     $("#fruittypesName").val(""); // 如果输入框为空就清空水果种类编号输入框
                 }
             })
         })

         // 根据水果种类编号查询水果种类名称
         function selectFruitTypeNameByFruitTypeId(fruittypesId, index) {
             $.ajax({
                 url: "/inventory/fruits/selectFruitTypeName",
                 dataType: "json",
                 data: {
                     "code": fruittypesId
                 },
                 type: "post",
                 success: function (resp) {
                     if (index != null) {
                         $("#fruitTypeNameSpan" + index).html(resp.fruitTypeName);
                     } else {
                         if (resp.fruitTypeName != null && resp.fruitTypeName !== "") {
                             $("#fruittypesName").val(resp.fruitTypeName);
                         } else {
                             $("#fruittypesName").val(""); // 如果查不到就清空水果种类名称输入框
                         }
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>
     <!--  下架水果  -->
     <script>
         // 下架水果
         function fruitBan(fruitId, isStatus) {
             let msgConfirm;
             if (isStatus === 0) {
                 msgConfirm = "确定要下架该水果吗?";
             } else {
                 msgConfirm = "确定要上架该水果吗?"
             }
             $.modal.confirm(msgConfirm, function() {
                 $.modal.loading("正在处理中，请稍后...");
                 setTimeout(function() {
                     fruitBans(fruitId, isStatus);
                 }, 1000); // 3000毫秒等于3秒
             });
         }
         function fruitBans(fruitId, isStatus) {
             $.ajax({
                 url: "/inventory/fruits/edit",
                 data: {
                     "fruitId": fruitId,
                     "isStatus": isStatus
                 },
                 dataType: "json",
                 type: "post",
                 success: function (resp) {
                     $.modal.closeLoading(); // 关闭遮罩层
                     $.modal.msgSuccess(resp.msg);
                     location.reload();
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

</body>
</html>